using Scanline.Core;
using System.Collections.Generic;
using UnityEngine;

namespace Scanline.GUI
{
    public class ScanController : MonoBehaviour
    {
        private Board board;
        private Point[] points;

        private void Awake()
        {
            board = Board.FindInstance();
            points = GetComponentsInChildren<Point>();
        }

        private void Update()
        {
            List<Vector2Int> positions = new List<Vector2Int>();
            for (int i = 0; i < points.Length; i++)
            {
                positions.Add(points[i].boardPosition);
            }
            List<Vector2Int> ret = new();
            ScanlineAlgorithm.Scan(positions, ret);
            board.SetInternelPixels(ret);
        }

        private void OnDrawGizmos()
        {
            if(!Application.isPlaying)
                points = GetComponentsInChildren<Point>();
            Gizmos.color = Color.red;
            for (int i = 0; i < points.Length - 1; i++)
            {
                Gizmos.DrawLine(points[i].aligned, points[i + 1].aligned);
            }
            Gizmos.DrawLine(points[^1].aligned, points[0].aligned);
        }
    }
}